package com.duobaoyu.dby.supply.message.listener;

import com.alibaba.fastjson.JSON;
import com.duobaoyu.dby.common.core.constant.RequestConstants;
import com.duobaoyu.dby.supply.biz.producer.constant.SupplyChainMQConstants;
import com.duobaoyu.dby.supply.common.mq.remote.message.base.BaseMessage;
import com.duobaoyu.dby.supply.core.rocketmq.listener.AbstractClusterListener;
import com.duobaoyu.dby.supply.message.constant.ConsumerGroupConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName SupplyChainOrderMessageConsumer
 * @Description 订单消息Consumer
 * @createTime 14:03:00 2023/10/10
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = SupplyChainMQConstants.SupplyChainTopic.ORDER_TOPIC, consumerGroup = ConsumerGroupConstant.ORDER)
public class SupplyChainOrderMessageConsumer extends AbstractClusterListener {

    @Override
    protected void beforeHandler(MessageExt message) {
		try {
			BaseMessage baseMessage = JSON.parseObject(new String(message.getBody()), BaseMessage.class);
			MDC.put(RequestConstants.REQUEST_ID, baseMessage.getTraceId());
		}catch (Exception e){
			MDC.put(RequestConstants.REQUEST_ID, message.getMsgId());
		}

        if(log.isDebugEnabled()) {
            log.debug("供应链消息处理订单Consumer接收到消息，messageId={},message={},topic = {},tag = {}", message.getMsgId(), message, message.getTopic(), message.getTags());
        }
    }

    @Override
    protected void finallyHandler(MessageExt message) {
        MDC.remove(RequestConstants.REQUEST_ID);
        if(log.isDebugEnabled()) {
            log.debug("供应链消息处理订单Consumer接收到消息，message={}", message);
        }
    }
}
